package com.ysq.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ysq.controller.result.Result;
import com.ysq.entity.Employee;
import com.ysq.mapper.EmployeeMapper;
import com.ysq.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * ClassName: EmployeeServiceImpl
 * Package: com.ysq.service.impl
 * Description:
 *
 * @Author YSQ
 * @Create 2023/1/2 - 8:43
 */
@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeMapper mapper;

    @Override
    public Result<Employee> login(Employee employee) {
        //1. 将页面提交的密码password进行md5加密处理
        String password = employee.getPassword();
        //通过工具类DigestUtils的md5DigestAsHex()方法进行md5加密处理
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        //2. 根据页面提交的用户名username查询数据库
        //要调用selectOne()方法, 需要传入条件
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Employee::getUsername, employee.getUsername());
        Employee emp = mapper.selectOne(lqw);

        //3. 如果没有查询到则返回登录失败结果
        if(emp == null){
            return Result.error("登录失败~~~");
        }

        //4. 密码比对，如果不一致则返回登录失败结果
        if(!emp.getPassword().equals(password)){
            return Result.error("登录失败~~~");
        }

        //5. 查看员工状态，如果为已禁用状态，则返回员工已禁用结果
        if(emp.getStatus() == 0){
            return Result.error("这号被封啦!!!");
        }

        //6. 登录成功
        return Result.success(emp);//封装了这个员工在后台查询到的所有信息
    }

    @Override
    public boolean save(HttpServletRequest request, Employee employee) {
        //在前端只能接收到账号username, 员工姓名name, 手机号phone, 性别sex, 身份证idNumber
        //其余数据需要手动设置
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));//设置初始密码为123456
//        employee.setCreateTime(LocalDateTime.now());//员工创建时间
//        employee.setUpdateTime(LocalDateTime.now());//员工信息更新时间
        //获得当前登录用户的id
//        Long empId = (Long) request.getSession().getAttribute("employee");
//        employee.setCreateUser(empId);//创建员工的管理员ID
//        employee.setUpdateUser(empId);//更新员工的管理员ID

        int row = mapper.insert(employee);//返回受影响的行数
        if (row > 0) {
            //添加成功
            return true;
        }
        //添加失败
        return false;
    }

    @Override
    public Page<Employee> selectPage(int page, int pageSize, String name) {
        //构造分页构造器 ==> 创建page分页对象, 设置分页参数
        Page<Employee> pageInfo = new Page<>(page, pageSize);
        //设置查询条件
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.like(name != null, Employee::getName, name);//模糊查询
        lqw.orderByDesc(Employee::getUpdateTime);//排序条件
        //执行查询
        mapper.selectPage(pageInfo, lqw);
        //查询完了之后 pageInfo 中就已经包含了分页相关的全部数据
        return pageInfo;
    }

    @Override
    public boolean update(HttpServletRequest request, Employee employee) {
        //获取当前登录用户ID, 要用来设置employee相关属性
//        Long empId = (Long) request.getSession().getAttribute("employee");
//        employee.setUpdateTime(LocalDateTime.now());//设置信息更新时间
//        employee.setUpdateUser(empId);//设置信息更新者ID

        //执行更新操作
        int row = mapper.updateById(employee);//返回受影响行数
        if (row > 0) {
            //更新成功
            return true;
        }
        //更新失败
        return false;
    }

    @Override
    public Employee dataEcho(Long id) {
        return mapper.selectById(id);
    }
}
